package cloud

import (
	"dgo/goutils/gohttp"
	"dgo/goutils/utils"
	"fmt"
	"gitee.com/ymofen/supervalue"
)

type PlgVxApiXcx struct {
	PlgBase
	appid     string
	appsecret string
}

func (this *PlgVxApiXcx) ReloadConf(conf *wrapper.SuperValue) error {

	if conf == nil {
		return nil
	}

	conf.CheckStrFunc("appid", func(strv string) {
		this.appid = strv
	})

	conf.CheckStrFunc("appsecret", func(strv string) {
		this.appsecret = strv
	})

	//this.mobilize_refresh()
	// 登录获取openid接口
	gohttp.HandleFunc(fmt.Sprintf("/%s/api/user/login", this.PlugId), this.onHttpLoginGetOpenid)

	// 获取用户手机号接口
	gohttp.HandleFunc(fmt.Sprintf("/%s/api/user/exinfo", this.PlugId), this.onHttpGetPhoneNumber)

	return nil
}

func (this *PlgVxApiXcx) OnObjectNotify(sender interface{}, notifytype int, data interface{}) (rval interface{}, err error) {
	if notifytype == utils.NOTIFY_TYPE_SETCONF {
		if conf, ok := data.(*wrapper.SuperValue); ok {
			err = this.ReloadConf(conf)
		} else {
			err = fmt.Errorf("非法的JSON配置")
		}
	} else if notifytype == utils.NOTIFY_TYPE_RELEASE {
		this.ReleaseFlag = 1
	}
	return
}

func CreateCloudVxApiXcxPlugFunc(args ...interface{}) (rval interface{}, err error) {
	obj := &PlgVxApiXcx{}
	if len(args) > 0 {
		if str, ok := args[0].(string); ok {
			obj.PlugId = str
		}
	}
	rval = obj
	return
}
